import {getOrderDetail, getRenewDetail, renewOrder} from "@src/api/deposit";
import router from "@src/router";

Component({
  data: {
    renewWay: 1,
    renewDetailData: null
  },
  methods: {
    onLoad(options) {
      this.setData({orderCode: options.orderCode});
      this.fetchData();
    },
    fetchData() {
      let data = {
        orderCode: this.data.orderCode
      };
      getRenewDetail(data).then(res => {
        this.setData({renewDetailData: res.data})
      })
      getOrderDetail(data).then(res => {
        if (res.code === 0) {
          //处理数据
          this.setData({order: res.data.order, reqGoodsData: res.data.goods})
        }
      })
    },
    handleChangeWay() {
      this.selectComponent("#chooseStoreDialog").open();
    },
    handleRenew() {
      wx.showLoading({title: "寄存中...", mask: true});
      let data = {
        expireTime: this.data.renewDetailData.expireTime,
        orderCode: this.data.orderCode,
        renewWay: this.data.renewWay
      };
      renewOrder(data).then(res => {
        wx.hideLoading();
        if (res.code === 0) {
          wx.showToast({title: "续存成功", icon: "success", duration: 1500});
          const eventChannel = this.getOpenerEventChannel()
          if (eventChannel && eventChannel.emit) {
            eventChannel.emit('renew');
          }
          setTimeout(() => {router.back()}, 1000);
        } else {
          wx.showToast({
            title:res.msg,
            icon: 'none',
            duration: 1500
          })
        }
      }).catch(() => {
        setTimeout(() => {router.back()}, 1000);
        wx.hideLoading();
      })
    }
  }
})
